package com.android.ailaoshan.common;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;

import com.android.ailaoshan.R;
import com.android.ailaoshan.welcome.WelcomeActivity;

import androidx.core.app.NotificationCompat;

public class MyNotification {

    private Context mContext;

    public MyNotification() {
        this.mContext = BaseApplication.getInstance().getbContext();
    }

    /**
     * API级别为26+时需要创建通道
     */
    public void createNotificationChanel(String chanel_id){

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            //通知管理面板显示的通知名称
            CharSequence name = mContext.getString(R.string.notification_name);

            //用户在系统设置中看到的描述
            String description = mContext.getString(R.string.notification_description);

            //通道重要性级别
            int importance = NotificationManager.IMPORTANCE_LOW;

            NotificationChannel channel = new NotificationChannel(chanel_id,name,importance);
            channel.setDescription(description);

            NotificationManager notificationManager = mContext.getSystemService(NotificationManager.class);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }
    }

    /**
     * 创建通知
     * @param contentID 通知内容字符串ID
     * @param tickerID 在通知栏弹出的内容字符串ID
     * @return notification对象
     */
    public Notification showNotification(int contentID, int tickerID, String chanel_id){

        Intent intent = new Intent(mContext, WelcomeActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext,0,intent,0);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext,chanel_id);
        notificationBuilder
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),R.mipmap.ic_launcher))
                .setContentTitle("哀牢山")
                .setContentText(mContext.getResources().getString(contentID))
                .setTicker(mContext.getResources().getString(tickerID))
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE);

        return notificationBuilder.build();
    }
}
